package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.*;


/**
 * Manejo de elementos de la entidad Periodo.<br>
 */
public class AccionesPeriodo extends CohesionAction {
    
/**
     * Acción llamada por Struts para ejecutar la acción PeriodoBuscarI.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoBuscarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "periodoBuscar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FPeriodoBuscar fFPeriodoBuscar = (FPeriodoBuscar)sess.getAttribute("FPeriodoBuscar");
             if (fFPeriodoBuscar==null) {
                 fFPeriodoBuscar = new FPeriodoBuscar();
                 sess.setAttribute("FPeriodoBuscar", fFPeriodoBuscar);
             } else {
                 fFPeriodoBuscar.reset(mapping, request);
             }
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PeriodoCrearI.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoCrearI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "periodoCrear";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FPeriodo fFPeriodo = (FPeriodo)sess.getAttribute("FPeriodo");
             if (fFPeriodo==null) {
                 fFPeriodo = new FPeriodo();
                 sess.setAttribute("FPeriodo", fFPeriodo);
             } else {
                 fFPeriodo.reset(mapping, request);
             }

            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PeriodoBuscar.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoBuscar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "PeriodoListarI";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPeriodoBuscar fFPeriodoBuscar = (FPeriodoBuscar)form;

            request.getSession().setAttribute("PeriodoBuscar", " where nombre like '%" + fFPeriodoBuscar.getNombre() + "%'");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PeriodoListarI.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoListarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "periodoListar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String crit = (String)request.getSession().getAttribute("PeriodoBuscar");
            if (crit==null) crit = "";
            else request.getSession().removeAttribute("PeriodoBuscar");
            java.util.List<Periodo> list = (java.util.List<Periodo>)s.createQuery("from Periodo"+crit).list();
            request.setAttribute("L_Periodo", list);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PeriodoI.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "periodo";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idPeriodo");
            Long idPeriodo = (id==null) ? 
                (Long)request.getSession().getAttribute("idPeriodo") :
                new Long(id);
            
            HttpSession sess = request.getSession();
            FPeriodo fFPeriodo = (FPeriodo)sess.getAttribute("FPeriodo");
            if (fFPeriodo==null) {
                fFPeriodo = new FPeriodo();
                sess.setAttribute("FPeriodo", fFPeriodo);
            } else {
                fFPeriodo.reset(mapping, request);
            }
            Periodo periodo = (Periodo)s.load(Periodo.class, idPeriodo);
            fFPeriodo.setIdPeriodo(periodo.getIdPeriodo());
            fFPeriodo.setNombre(periodo.getNombre());
            fFPeriodo.setFechaInicio(periodo.getFechaInicio().toString());
            fFPeriodo.setFechaFinal(periodo.getFechaFinal().toString());

            request.getSession().setAttribute("idPeriodo", idPeriodo);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PeriodoAct.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoAct(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "periodo";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPeriodo fFPeriodo = (FPeriodo)form;

            Periodo periodo = (Periodo)s.load(Periodo.class, new Long(fFPeriodo.getIdPeriodo()));
            periodo.setNombre(fFPeriodo.getNombre());
            periodo.setFechaInicio(new ve.usb.cohesion.runtime.LocalDate(fFPeriodo.getFechaInicio()));
            periodo.setFechaFinal(new ve.usb.cohesion.runtime.LocalDate(fFPeriodo.getFechaFinal()));


            request.getSession().setAttribute("idPeriodo", periodo.getIdPeriodo());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PeriodoElim.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoElim(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "PeriodoListarI";
        final String SALIDA_1 = "periodo";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idPeriodo");
            Long idPeriodo = (id==null) ? 
                (Long)request.getSession().getAttribute("idPeriodo") :
                new Long(id);
            Periodo periodo = (Periodo)s.load(Periodo.class, idPeriodo);
            s.delete(periodo);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
                request.setAttribute("msg", "No se pudo eliminar entidad.");
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PeriodoCrear.
     * Registro de la creación de una periodo.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PeriodoCrear(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_1 = "periodo";

        String salida = SALIDA_1;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPeriodo fFPeriodo = (FPeriodo)form;

            Periodo periodo = new Periodo();
            periodo.setNombre(fFPeriodo.getNombre());
            periodo.setFechaInicio(new ve.usb.cohesion.runtime.LocalDate(fFPeriodo.getFechaInicio()));
            periodo.setFechaFinal(new ve.usb.cohesion.runtime.LocalDate(fFPeriodo.getFechaFinal()));

            s.save(periodo);
            s.flush();
            fFPeriodo.setIdPeriodo(periodo.getIdPeriodo());

            request.getSession().setAttribute("idPeriodo", periodo.getIdPeriodo());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }



}